package com.huadao.cart_ccs.modle;

import com.cart.protocol.CommandCreateFactory;
import com.cart.protocol.ProtocolDefine;
import com.huadao.cart_ccs.base.BaseSerialPortModel;
import com.huadao.cart_ccs.common.ByteUtils;
import com.huadao.cart_ccs.data.bo.CommandWord;
import com.huadao.cart_ccs.data.bo.PeristalticPumpBO;
import com.huadao.cart_ccs.data.bo.Protocol;

/**
 * 蠕动泵
 */
public class PeristalticPumpModle extends BaseSerialPortModel {

    /**
     * 打开蠕动泵
     */
    public void openPeristaltic_pump(byte modular, int speed, byte direction) {
        byte[] speedByte = ByteUtils.int2bytesLG(2, speed);
        setPeristalticPump(modular, PeristalticPumpBO.star, speedByte, direction);
    }

    /**
     * 关闭蠕动泵
     */
    public void closePeristaltic_pump(byte modular) {
        setPeristalticPump(modular, PeristalticPumpBO.stop, null, (byte) 0);
    }

    /**
     * 读取蠕动泵
     */
    public void readPeristaltic_pump() {
        String readFeedLiquorPump = CommandCreateFactory.buildReadModuleStatus(ProtocolDefine.ModuleName.FEED_LIQUOR_PUMP);
        serialPortRequest(readFeedLiquorPump);
    }


    /**
     * 读取  进液泵
     */
    public void readFeedLiquorPump() {
        String readFeedLiquorPump = CommandCreateFactory.buildReadModuleStatus(ProtocolDefine.ModuleName.FEED_LIQUOR_PUMP);
        serialPortRequest(readFeedLiquorPump);
    }


    private void setPeristalticPump(byte modularNO, byte order, byte[] speed, byte direction) {
        byte[] orderContent;
        if (order == PeristalticPumpBO.star) {//启动
            orderContent = new byte[speed.length + 1];
            for (int i = 0; i < orderContent.length - 1; i++) {
                orderContent[i] = speed[i];
            }
            orderContent[2] = direction;
        } else {//停止
            orderContent = null;
        }
        Protocol params = ByteUtils
                .generateProtocol(CommandWord.write, new byte[]{PeristalticPumpBO.ModularType_PeristalticPump, modularNO, order}, orderContent);
        serialPortRequest(params);
    }
}
